package com.jeff.rps.ui;

import java.awt.Color;
import java.awt.Font;

import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;

import com.jeff.rps.Player;
import com.jogamp.opengl.util.awt.TextRenderer;

public class IntroEventListener implements GLEventListener
{
  private final static Color P1_COLOR = Color.CYAN;
  private final static Color P2_COLOR = Color.GREEN;
  private final static Color VS_COLOR = Color.RED;

  private final Player player1;
  private final Player player2;

  public IntroEventListener(Player player1, Player player2)
  {
    this.player1 = player1;
    this.player2 = player2;
  }

  public void display(GLAutoDrawable canvas)
  {
    drawNames(canvas);
  }

  private void drawNames(GLAutoDrawable canvas)
  {
    int threeQuarterHeight = 3 * canvas.getHeight() / 4;
    int midHeight = canvas.getHeight() / 2;
    int quarterHeight = canvas.getHeight() / 4;
    int buffer = 20;

    TextRenderer text = new TextRenderer(new Font("ARIAL", Font.PLAIN, 32));
    text.beginRendering(canvas.getWidth(), canvas.getHeight());

    text.setColor(P1_COLOR);
    text.draw(player1.getName(), buffer, threeQuarterHeight);

    text.setColor(VS_COLOR);
    text.draw("VS", buffer * 5, midHeight);

    text.setColor(P2_COLOR);
    text.draw(player2.getName(), buffer * 10, quarterHeight);

    text.endRendering();
  }

  public void init(GLAutoDrawable canvas)
  {
    final GL2 gl = canvas.getGL().getGL2();
    final GLU glu = new GLU();

    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluOrtho2D(-1.0f, 1.0f, -1.0f, 1.0f);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();

    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  }

  public void reshape(GLAutoDrawable canvas, int x, int y, int width, int height)
  {
    // TODO Auto-generated method stub
  }

  public void dispose(GLAutoDrawable canvas)
  {
    // TODO Auto-generated method stub
  }

}
